package com.hzmg.web.starter;

import com.google.common.base.Stopwatch;
import com.hzmg.web.creater.WebApiCreater;
import com.hzmg.web.utils.NetUtils;
import io.vertx.core.Vertx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * vert.x starter
 *
 * @author zbw
 */
@Slf4j
public class VertXStarter implements ApplicationContextAware {
    public static Vertx vertx;
    private ApplicationContext springContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.springContext = applicationContext;
    }

    @PostConstruct
    public void init() {
        Stopwatch stopwatch = Stopwatch.createStarted();
        log.info("[Vertx web] Vertx web's vert.x system start to bootstrap...");
        int port = springContext.getEnvironment().getProperty("server.port", Integer.class);
        String localIP = NetUtils.getLocalHost();
        String address = localIP + ":" + port;
        log.info("[Vertx web] vert.x server address: {}", address);
        vertx = Vertx.vertx();
        vertx.deployVerticle(new WebApiCreater(springContext));
        log.info("[Vertx web] Vertx web's vert.x system started successfully, using time {}.", stopwatch);
    }
}
